import { Transform } from "./engine/Transform";
import { Behaviour } from "./engine/Behaviour";
import { Sound } from "./Sound";
import { BitmapRenderer } from "./engine/BitmapRenderer";
import { UIfollow } from "./UIfollow";

export class DisableOnClick extends Behaviour {

    isClicked: boolean;
    turnId: String = null;
    partentId: String;
    isturn = false;

    onStart() {
        this.gameObject.onClick = () => {
            if (this.gameObject.id == 'song') {
                const isPlay = this.gameObject.getBehaviour(Sound).onPlay;
                if (isPlay) {
                    this.gameObject.getBehaviour(Sound).onPlay = false;
                    this.gameObject.getBehaviour(Transform).alpha = 0.5;
                }
                else {
                    this.gameObject.getBehaviour(Sound).onPlay = true;
                    this.gameObject.getBehaviour(Transform).alpha = 1;
                }
            }
            else if (this.gameObject.id == 'help' && !this.isClicked) {
                this.gameObject.getBehaviour(UIfollow).indexX = 95;
                this.gameObject.getBehaviour(Transform).x = 95;
                this.gameObject.getBehaviour(Transform).y = 100;
                this.gameObject.getBehaviour(BitmapRenderer).source = "assets/click/instruction.png";
                this.isClicked = true;
                console.log(this.gameObject);
            }
            else if (this.gameObject.id == 'help' && this.isClicked) {
                this.gameObject.getBehaviour(UIfollow).indexX = 650;
                this.gameObject.getBehaviour(Transform).x = 650;
                this.gameObject.getBehaviour(Transform).y = 0;
                this.gameObject.getBehaviour(BitmapRenderer).source = "assets/click/help.png";
                this.isClicked = false;
            }
            else if (this.gameObject.id == 'turn') {
                this.partentId = this.gameObject.parent.id
                const toTurn = this.turnId.concat(".json")
                const sceneRootGameObject = this.engine.getGameObject('sceneRoot');
                const prefabGameObject = this.engine.createPrefab(toTurn);
                sceneRootGameObject.addChild(prefabGameObject);
                this.isturn = true;
            }
        };
    }

    onUpdate() {
        if (this.isturn) {
            // if (this.partentId != 'menu') {
            //     const sceneRootGameObject = this.engine.getGameObject('sceneRoot');
            //     const prefabGameObject = this.gameObject.parent
            //     sceneRootGameObject.removeChild(prefabGameObject);
            // }
            // this.isturn = false
            const sceneRootGameObject = this.engine.getGameObject('sceneRoot');
            const prefabGameObject = this.gameObject.parent
            sceneRootGameObject.removeChild(prefabGameObject);
        }
    }

}
